package com.gps.pe.dao;

import static com.gps.pe.utility.DAOUtil.prepareStatement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.gps.pe.exception.DAOException;
import com.gps.pe.model.User;
import com.gps.pe.utility.GetSQLQuery;

/**
 * Class have implementation details of User DAO
 * 
 * @author GPS
 *
 */
public class UserDAOImpl implements IUserDAO {

	private static final Logger LOGGER = Logger.getLogger(UserDAOImpl.class);
	private Connection connection = null;

	public UserDAOImpl(Connection connection) {
		this.connection = connection;
	}

	/**
	 * This method returns the object of User based on user id.
	 * 
	 * @return user
	 */
	@Override
	public User getUserByLoginId(String loginId) {

		User user = null;
		String query = UserQuery.GET_USER_BY_LOGINID.getKeyVal();
		query = GetSQLQuery.getQuery(query);

		LOGGER.debug("SQL for user is [" + query + "]");

		Object[] values = { loginId };

		try (PreparedStatement pstmt = prepareStatement(connection, query, values);
				ResultSet rset = pstmt.executeQuery();) {

			if (rset.next()) {
				LOGGER.debug("User found for id [" + loginId + "].");
				user = mapUser(rset);
				LOGGER.debug("Result Set fetched for login ID [" + loginId + "]");
			} else {
				LOGGER.debug("User not for for login ID" + loginId + "]");
			}

		} catch (SQLException se) {
			LOGGER.error("Exception occured:", se);
			throw new DAOException("Unable to get the result ", se);
		}

		return user;
	}

	/**
	 * 
	 * @param resultSet
	 * @throws DAOException
	 */
	private User mapUser(final ResultSet resultSet) throws DAOException {
		User user = new User();
		try {
			user.setUuid(resultSet.getString("uuid"));
			user.setLoginId(resultSet.getString("login_id"));
			user.setPassword(resultSet.getString("password"));
			user.setRoleId(resultSet.getInt("role_roleId"));
			user.setVersion(resultSet.getInt("version"));
			user.setDeleted(resultSet.getBoolean("isDeleted"));
		} catch (SQLException e) {
			LOGGER.error("Error while getting user: ", e);
			throw new DAOException("Exception occured to get user detail: ", e);
		}
		return user;
	}

	@Override
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub

	}

	@Override
	public User updateUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User deleteUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Enum to get Query from property file
	 */
	private enum UserQuery {
		GET_USER_BY_LOGINID("pe.user.getUserByLoginId");

		private String key;

		UserQuery(String key) {
			this.key = key;
		}

		String getKeyVal() {
			return key;
		}
	}

}
